﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.WebControl.DWZAdapter;
using System.Web.Mvc;
using Resto.CostControl.Common.Consts;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Query.SystemModule.ViewObject;
using Resto.CostControl.Core.Query.MaterialModule.QueryObject;
using Enterprise.Framework.Core.Container;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.DomainModel.Repositories;

namespace Resto.CostControl.Core.Facade
{
    public class DicEnumService : IEnumService
    {

        public IList<SelectListItem> GetEnumDataSource(string enumKey)
        {
            if (enumKey == DictionaryTypeConsts.System_DictionaryType_Code)
            {
                List<SelectListItem> list = new List<SelectListItem>();
                for (int i = 0; i < DictionaryTypeConsts.Array_DictionaryTypeCode.Length; i++)
                    list.Add(new SelectListItem
                    {
                        Text = DictionaryTypeConsts.Array_DictionaryTypeName[i],
                        Value = DictionaryTypeConsts.Array_DictionaryTypeCode[i]
                    });

                return list;
            }

            if (DictionaryTypeConsts.Array_DictionaryTypeCode.Contains(enumKey))
            {
                DictionaryVO[] array = DictionaryVOArrayCache.Get(enumKey);
                List<SelectListItem> list = new List<SelectListItem>();

                if (array == null || array.Length == 0)
                    return list;

                foreach (DictionaryVO dic in array)
                    list.Add(new SelectListItem { Text = dic.Name, Value = dic.Code });

                return list;
            }

            // return SelectListQueryHelper.QuerySelectListItemByType(enumKey);

            if (enumKey == "Material")
            {
                IEntityRepository<Material> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<Material>();
                var items = repository.Objects
                    .Where(o => o.IsDelete == false)
                    .Select(m => new SelectListItem { Text = m.Name, Value = m.ID.ToString() })
                    .ToList();

                return items;
            }

            return new List<SelectListItem>();
        }

        public string GetEnumText(string enumKey, string value)
        {
            throw new NotImplementedException();
        }
    }
}
